package com.service.impl;


import com.dao.StudentDao;
import com.dao.impl.StudentDaoImpl;
import com.pojo.PagePojo;
import com.pojo.Student;
import com.service.StudentService;

import java.util.List;

/**
 * @author zxl
 * @create :2022/9/28
 */
//Service接口的实现类
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao = new StudentDaoImpl();
//查询全部
    @Override
    public List<Student> getStudentAll() {
        return studentDao.selectByAll();
    }
//删除
    @Override
    public boolean removeStudent(Student student) {
        return studentDao.deleteStudent(student) > 0;
    }
//添加
    @Override
    public boolean addStudent(Student student) {
        return studentDao.insertStudnet(student) > 0;
    }
//查id
    @Override
    public Student getStudentById(Student student) {
        return studentDao.selectById(student);
    }
//修改
    @Override
    public boolean modifyStudentById(Student student) {
        return studentDao.updateStudentById(student) > 0;
    }

    //分页

    /**
     * @Override public PagePojo<Student> selectByPage(int page, int pageSize) {
     * //计算索引
     * int begin = (page-1)*pageSize;
     * <p>
     * //分页查询
     * List<Student> students = studentDao.selectByPage(begin, pageSize);
     * <p>
     * //总数
     * int count = studentDao.selectCount();
     * <p>
     * //返回的包含了总数和数据对象
     * PagePojo<Student> studentPagePojo = new PagePojo<>();
     * studentPagePojo.setCount(count);
     * studentPagePojo.setDatas(students);
     * <p>
     * return studentPagePojo;
     * }
     */

    //带条件分页查询

    @Override
    public PagePojo<Student> selectByPage(int page, int pageSize, Student student) {
        //计算索引
        int begin = (page - 1) * pageSize;


        //分页查询
        List<Student> students = studentDao.selectByPage(begin, pageSize, student);

        //总数  带条件的查询
        int count = studentDao.selectCount(student);

        //返回的包含了总数和数据对象
        PagePojo<Student> studentPagePojo = new PagePojo<>();
        studentPagePojo.setDatas(students);
        studentPagePojo.setCount(count);

        return studentPagePojo;
    }
}
